package com.zyd.shiro.controller.admin;
import java.util.List;
import java.util.Map;

import com.zyd.shiro.business.service.MallChannelService;
import com.zyd.shiro.business.service.impl.ChannelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.Channel;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
/**
 * 控制器层
 * @author RookieWZW
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/channel")
public class ChannelController {
    @Autowired
    private MallChannelService channelService;
    @Autowired
    private ChannelServiceImpl channelServicei;



    @RequestMapping(value="/getChannel/{id}",method=RequestMethod.GET)
    public ResponseVO getChannelByUserId(@PathVariable Integer id){

        return ResultUtil.success("查询成功", channelServicei.getChannelByUserId(id));

    }
    /**
     * 查询全部数据
     * @return
     */
    @RequestMapping(value="/",method=RequestMethod.GET)
    public ResponseVO findAll(){
        return ResultUtil.success("查询成功",channelService.findAll());

    }

    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.GET)
    public ResponseVO findOne(@PathVariable Integer id){
        return ResultUtil.success("查询成功", channelService.findOne(id));

    }

    /**
     * 分页查询全部数据
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
    public ResponseVO findPage(@PathVariable int page,@PathVariable int size){
        Page<Channel> pageList = (Page<Channel>) channelService.findPage(page, size);
        return ResultUtil.success("查询成功",new PageResult(pageList.getTotalElements(), pageList.getContent() ) );
    }

    /**
     * 分页+多条件查询
     * @param searchMap 查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
    public ResponseVO findSearch(@RequestBody Map searchMap ,@PathVariable int page,@PathVariable int size){
        Page<Channel> pageList = (Page<Channel>) channelService.findSearch(searchMap, page, size);
        return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );
    }

    /**
     * 增加
     * @param channel
     */
    @RequestMapping(value="/",method=RequestMethod.POST)
    public ResponseVO add(@RequestBody Channel channel  ){
        channelService.add(channel);
        return ResultUtil.success("增加成功");
    }

    /**
     * 修改
     * @param channel
     */
    @RequestMapping(value="/{id}",method=RequestMethod.PUT)
    public ResponseVO update(@RequestBody Channel channel,@PathVariable Integer id ){
        channel.setId(id);
        channelService.update(channel);
        return ResultUtil.success("修改成功");
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public ResponseVO delete(@PathVariable Integer id ){
        channelService.delete(id);
        return ResultUtil.success("删除成功");
    }
}
